package hos.kotlin

import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.GregorianCalendar

/**
 * <p>Title: Calendar </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2024-07-11 16:45
 * @version : 1.0
 */
object TimeUtils {

    @JvmStatic
    @JvmOverloads
    fun getCalendar(millis: Long = System.currentTimeMillis()): Calendar {
        val gregorianCalendar = GregorianCalendar()
        gregorianCalendar.timeInMillis = millis
        return gregorianCalendar
    }

    @JvmStatic
    @JvmOverloads
    fun time(target: String?, value: String = ""): String {
        return target.time(value)
    }

    @JvmStatic
    @JvmOverloads
    fun timeAndroid(target: String?, value: String = ""): String {
        var time = time(target, value)
        if (time.isNull()) {
            return value
        }
        if (time.contains("/")) {
            time = time.replace("/", "-")
        }
        if (!time.contains(".")) {
            return time;
        }
        return time.substring(0, time.lastIndexOf("."))
    }

    @JvmStatic
    @JvmOverloads
    fun timeIos(target: String?, value: String = ""): String {
        var time = time(target, value)
        if (time.isNull()) {
            return value
        }
        if (time.contains("-")) {
            time = time.replace("-", "/")
        }
        if (!time.contains(".")) {
            return time;
        }
        return time.substring(0, time.lastIndexOf("."))
    }

    @Suppress("SimpleDateFormat")
    @JvmStatic
    @JvmOverloads
    fun getSafeDateFormat(pattern: String = TimeFormat.full): SimpleDateFormat {
        return SimpleDateFormat(pattern)
    }


    /**
     * Return the current time in milliseconds.
     *
     * @return the current time in milliseconds
     */
    @JvmStatic
    fun getNowMills(): Long {
        return System.currentTimeMillis()
    }


    /**
     * Return the current formatted time string.
     *
     * @param format The format.
     * @return the current formatted time string
     */
    @JvmStatic
    @JvmOverloads
    fun getNowString(format: String = TimeFormat.full): String {
        return System.currentTimeMillis().time(format)
    }

    //// 获取当前的时间 Now Date Str.(yyyy-MM-dd 00:00:00)
    @JvmStatic
    fun getNowStringStart(): String {
        return getNowString().calendar().startDay().string()
    }

    //// 获取当前的时间 Now Date Str.(yyyy-MM-dd 00:00:00)
    @JvmStatic
    fun getNowStringEnd(): String {
        return getNowString().calendar().endDay().string()
    }

    /**
     * Formatted time string to the milliseconds.
     *
     * @param time    The formatted time string.
     * @param pattern The pattern of date format, such as yyyy-MM-dd HH:mm:ss
     * @return the milliseconds
     */
    @JvmStatic
    @JvmOverloads
    fun string2Millis(
        time: String?,
        pattern: String = TimeFormat.full,
        value: String = ""
    ): Long {
        val empty: String = time(time, value)
        if (empty.isEmpty()) {
            return -1
        }
        try {
            return string2MillisFormat(
                empty,
                getSafeDateFormat(pattern)
            )
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return -1
    }

    @JvmStatic
    @JvmOverloads
    fun string2MillisFormat(
        time: String,
        format: DateFormat = getSafeDateFormat(TimeFormat.full)
    ): Long {
        try {
            return format.parse(time).time
        } catch (var3: ParseException) {
            var3.printStackTrace()
            return -1L
        }
    }

    /**
     * Formatted time string to the milliseconds.
     *
     * @param time    The formatted time string.
     * @param pattern The pattern of date format, such as yyyy-MM-dd HH:mm:ss
     * @return the milliseconds
     */
    @JvmStatic
    @JvmOverloads
    fun string2Calendar(
        time: String?,
        pattern: String = TimeFormat.full,
        value: Calendar = GregorianCalendar()
    ): Calendar {
        val string2Millis: Long = string2Millis(time, pattern)
        if (string2Millis == -1L) {
            return value
        }
        return getCalendar(string2Millis)
    }


    /**
     * Milliseconds to the formatted time string.
     *
     * @param millis  The milliseconds.
     * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
     * @return the formatted time string
     */
    @JvmStatic
    @JvmOverloads
    fun millis2String(
        millis: Long,
        pattern: String = TimeFormat.full,
    ): String {
        return millis2StringFormat(millis, getSafeDateFormat(pattern))
    }

    @JvmStatic
    @JvmOverloads
    fun millis2StringFormat(
        millis: Long,
        format: DateFormat = getSafeDateFormat(TimeFormat.full)
    ): String {
        return format.format(Date(millis))
    }

    @JvmStatic
    fun millis2StringStart(millis: Long): String {
        return millis2String(millis, "yyyy-MM-dd") + " 00:00:00"
    }

    @JvmStatic
    fun millis2StringEnd(millis: Long): String {
        return millis2String(millis, "yyyy-MM-dd") + " 23:59:59"
    }

    fun convert(
        time: String?,
        from: String = TimeFormat.full,
        to: String = TimeFormat.full
    ): String {
        return time.calendar(from).string(to)
    }
}

object TimeFormat {
    const val full_s: String = "yyyyMMddHHmmssSSS"
    const val full: String = "yyyy-MM-dd HH:mm:ss"
    const val y_mo_d_h_m: String = "yyyy-MM-dd HH:mm"
    const val y_mo_d_h: String = "yyyy-MM-dd HH"
    const val y_mo_d: String = "yyyy-MM-dd"
    const val y_mo: String = "yyyy-MM"
    const val y: String = "yyyy"
    const val mo_d: String = "MM-dd"
    const val mo_d_h_m: String = "MM-dd HH:mm"
    const val h_m_s: String = "HH:mm:ss"
    const val h_m: String = "HH:mm"

    const val zh_full: String = "yyyy年MM月dd日 HH时mm分ss秒"
    const val zh_y_mo_d_h_m: String = "yyyy年MM月dd日 HH时mm分"
    const val zh_y_mo_d_h: String = "yyyy年MM月dd日 HH时"
    const val zh_y_mo_d: String = "yyyy年MM月dd日"
    const val zh_y_mo: String = "yyyy年MM月"
    const val zh_y: String = "yyyy年"
    const val zh_mo_d: String = "MM月dd日"
    const val zh_mo_d_h_m: String = "MM月dd日 HH时mm分"
    const val zh_h_m_s: String = "HH时mm分ss秒"
    const val zh_h_m: String = "HH时mm分"

    const val ios_full: String = "yyyy/MM/dd HH:mm:ss"
    const val ios_y_mo_d_h_m: String = "yyyy/MM/dd HH:mm"
    const val ios_y_mo_d_h: String = "yyyy/MM/dd HH"
    const val ios_y_mo_d: String = "yyyy/MM/dd"
    const val ios_y_mo: String = "yyyy/MM"
    const val ios_y: String = "yyyy"
    const val ios_mo_d: String = "MM/dd"
    const val ios_mo_d_h_m: String = "MM/dd HH:mm"
    const val ios_h_m_s: String = "HH:mm:ss"
    const val ios_h_m: String = "HH:mm"
}
